package com.example;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.annotation.Resource;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

import java.util.concurrent.Executor;

@MapperScan(basePackages = "com.example.mapper")
@SpringBootApplication
public class DlykServerApplication implements CommandLineRunner {
	@Resource
	private RedisTemplate redisTemplate;
//	UsernamePasswordAuthenticationFilter usernamePasswordAuthenticationFilter;

	public static void main(String[] args) {
		ConfigurableApplicationContext context = SpringApplication.run(DlykServerApplication.class, args);
		String[] beanNamesForType = context.getBeanNamesForType(Executor.class);
		for (String s : beanNamesForType) {
			System.out.println(s);
		}
		Object applicationTaskExecutor = context.getBean("applicationTaskExecutor");
		System.out.println(applicationTaskExecutor);
	}


	@Override
	public void run(String... args) throws Exception {
//		修改一下key和value的序列化方式

		//设置key序列化
		redisTemplate.setKeySerializer(new StringRedisSerializer());

		//对象映射工具，Java对象 和 json对象进行相互转化
		ObjectMapper mapper = new ObjectMapper();
		//设置可见性
		mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
		//激活类型
		mapper.activateDefaultTyping(mapper.getPolymorphicTypeValidator(), ObjectMapper.DefaultTyping.EVERYTHING);

		//设置value序列化
		redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<Object>(mapper, Object.class));

		//设置hashKey序列化
		redisTemplate.setHashKeySerializer(new StringRedisSerializer());

		//设置haskValue序列化
		redisTemplate.setHashValueSerializer(new Jackson2JsonRedisSerializer<Object>(mapper, Object.class));
	}
}
